package com.wtwd.device.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.dao.HeartRateMapper;
import com.wtwd.device.model.entity.HeartRateInfo;
import com.wtwd.device.model.entity.HeartRateRecord;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.HeartRateService;
import com.wtwd.device.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author mjy
 * @date 2021/09/02
 * @description
 */
@Service
public class HeartRateServiceImpl implements HeartRateService {

    @Autowired
    private HeartRateMapper heartRateMapper;


    @Override
    public RespEntity getDaysHeartRateList(Integer deviceId, String startTime, String endTime) {
        List<HeartRateRecord> heartRateLists = heartRateMapper.getDaysHeartRateList(deviceId, startTime, endTime);
        if (heartRateLists != null && heartRateLists.size() > 0) {
            heartRateLists.stream().forEach(heartRateRecord -> {
                List<HeartRateInfo> heartRateInfos = JSONArray.parseArray(heartRateRecord.getHeartRateInfo(), HeartRateInfo.class);
                heartRateRecord.setListHeartRate(heartRateInfos);
            });
        }
        return new RespEntity(RespCode.SUCCESS, heartRateLists);
    }

    @Override
    public RespEntity getMonthHeartRateList(Integer deviceId, String month) {
        List<HeartRateRecord> heartRateLists = heartRateMapper.getMonthHeartRateList(deviceId, month);
        if (heartRateLists != null && heartRateLists.size() > 0) {
            heartRateLists.stream().forEach(heartRateRecord -> {
                List<HeartRateInfo> heartRateInfos = JSONArray.parseArray(heartRateRecord.getHeartRateInfo(), HeartRateInfo.class);
                heartRateRecord.setListHeartRate(heartRateInfos);
            });
        }
        heartRateLists.forEach(e-> System.out.println(DateUtils.format(e.getTime(),DateUtils.dateFormat5)));
        return new RespEntity(RespCode.SUCCESS, heartRateLists);
    }

    /**
     * 一天的记录只会上传一次
     *
     * @param heartRateRecord
     * @return
     */
    @Override
    public RespEntity addHeartRate(HeartRateRecord heartRateRecord) {
        //鉴别今天的数据是否已经上传
        String time = DateUtils.format(heartRateRecord.getTime(), DateUtils.dateFormat5);
        HeartRateRecord heartRateRecordOld = heartRateMapper.getHeartRateByDeviceIdAndTime(heartRateRecord.getDeviceId(), time);
        if (heartRateRecordOld != null) {
            return new RespEntity(RespCode.TODAYS_DATA_ALREADY_EXIST);
        }
        //数据转化
        heartRateRecord.setHeartRateInfo(JSONArray.toJSONString(heartRateRecord.getListHeartRate()));
        heartRateRecord.setUploadTime(new Date().getTime());
        //上传数据
        Integer rest = heartRateMapper.addHeartRate(heartRateRecord);
        return new RespEntity(rest>0?RespCode.SUCCESS:RespCode.FAIL);
    }

    @Override
    public Integer clearHeartRateByDate(String date) {
        return heartRateMapper.clearHeartRateByDate(date);
    }

    @Override
    public HeartRateRecord getHeartRateByDeviceIdAndTime(Long id, String time) {
        return heartRateMapper.getHeartRateByDeviceIdAndTime(id,time);
    }

    @Override
    public Integer deleteHeartRateByDeviceId(Long id) {
        return heartRateMapper.deleteHeartRateByDeviceId(id);
    }
}
